package com.niiwoo.civet.trade.service.local.databackflow;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dto.request.databackflow.AbortiveProjectBackFlowRequestDTO;
import com.niiwoo.civet.trade.dto.request.databackflow.ProjectStatusChangeBackFlowRequestDTO;
import com.niiwoo.civet.trade.service.local.common.ProjectCommonService;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.tiancheng.component.TianchengDataFlowService;
import com.niiwoo.tripod.tiancheng.request.df.AbortiveProjectDataFlowRequest;
import com.niiwoo.tripod.tiancheng.request.df.ProjectStatusChangeDataFlowRequest;
import com.niiwoo.tripod.tiancheng.response.common.TianchengUapResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 天称数据回流本地服务
 *
 * @author TomXue
 * @since 2018-04-09
 */
@Service
@Slf4j
public class TianChengDataBackFlowLocalService {
    @Autowired
    ProjectCommonService projectCommonService;
    @Autowired
    TianchengDataFlowService tianchengDataFlowService;

    public void abortiveProject(AbortiveProjectBackFlowRequestDTO abortiveProjectBackFlowRequestDTO) {
        try {
            Project project = projectCommonService.queryProjectBasicInfo(Long.valueOf(abortiveProjectBackFlowRequestDTO.getBid()));
            if (project == null) {
                log.error("流标事件回流,标的ID={},发生异常,原因是标的不存在.", abortiveProjectBackFlowRequestDTO.getBid());
                return;
            }

            AbortiveProjectDataFlowRequest abortiveProjectDataFlowRequest = new AbortiveProjectDataFlowRequest();
            abortiveProjectDataFlowRequest.setCause(abortiveProjectBackFlowRequestDTO.getCause());
            abortiveProjectDataFlowRequest.setDetail(abortiveProjectBackFlowRequestDTO.getDetail());
            abortiveProjectDataFlowRequest.setProjectId(abortiveProjectBackFlowRequestDTO.getBid());

            log.info("流标事件回流,标的ID={},发起天称请求参数={}", abortiveProjectBackFlowRequestDTO.getBid(), JSON.toJSONString(abortiveProjectDataFlowRequest));
            CompletableFuture<TianchengUapResponse> abortiveProjectCompletableFuture = tianchengDataFlowService.abortiveProject(abortiveProjectDataFlowRequest);
            TianchengUapResponse tianchengUapResponse = abortiveProjectCompletableFuture.get(3, TimeUnit.SECONDS);
            log.info("流标事件回流,标的ID={},天称响应结果={}", abortiveProjectBackFlowRequestDTO.getBid(), JSON.toJSONString(tianchengUapResponse));
        } catch (Exception e) {
            log.error("流标事件回流,标的ID={},发生系统异常,原因={}", abortiveProjectBackFlowRequestDTO.getBid(), e);
        }
    }

    public void projecctStatusChange(ProjectStatusChangeBackFlowRequestDTO projectStatusChangeBackFlowRequestDTO) {
        try {
            Project project = projectCommonService.queryProjectBasicInfo(projectStatusChangeBackFlowRequestDTO.getProjectId());
            if (project == null) {
                log.error("标的状态变更事件回流,标的ID={},发生异常,原因是标的不存在.", projectStatusChangeBackFlowRequestDTO.getProjectId());
                throw new BizException("TRD20001");
            }

            ProjectStatusChangeDataFlowRequest projectStatusChangeDataFlowRequest = new ProjectStatusChangeDataFlowRequest();
            projectStatusChangeDataFlowRequest.setProjectId(String.valueOf(project.getProjectId()));
            projectStatusChangeDataFlowRequest.setUpdateTime(String.valueOf(System.currentTimeMillis() * 1000));
            projectStatusChangeDataFlowRequest.setFromType(convertProjectStatus4TianCheng(projectStatusChangeBackFlowRequestDTO.getFromStatus()));
            projectStatusChangeDataFlowRequest.setFullTime(projectStatusChangeBackFlowRequestDTO.getFullTime());
            projectStatusChangeDataFlowRequest.setType(convertProjectStatus4TianCheng(projectStatusChangeBackFlowRequestDTO.getStatus()));

            log.info("标的状态变更事件回流,标的ID={},发起天称请求参数={}", projectStatusChangeBackFlowRequestDTO.getProjectId(), JSON.toJSONString(projectStatusChangeDataFlowRequest));
            CompletableFuture<TianchengUapResponse> projectStatusChangeCompletableFuture = tianchengDataFlowService.projectStatusChange(projectStatusChangeDataFlowRequest);
            TianchengUapResponse tianchengUapResponse = projectStatusChangeCompletableFuture.get(3, TimeUnit.SECONDS);
            log.info("标的状态变更事件回流,标的ID={},天称响应结果={}", projectStatusChangeBackFlowRequestDTO.getProjectId(), JSON.toJSONString(tianchengUapResponse));
        } catch (Exception e) {
            log.error("标的状态变更事件回流,标的ID={},发生系统异常,原因={}", projectStatusChangeBackFlowRequestDTO.getProjectId(), e);
        }
    }

    private String convertProjectStatus4TianCheng(String projectStatus) {
        if (StringUtils.isEmpty(projectStatus)) {
            return null;
        }

        switch (projectStatus) {
            case "100":
                return "0";
            case "200":
                return "4";
            case "300":
                return "5";
            case "400":
            case "401":
            case "403":
                return "6";
            case "402":
                return "45";
            case "301":
                return "43";
            case "302":
                return "44";
            case "504":
                return "33";
            case "500":
            case "501":
            case "502":
            case "503":
            case "505":
            case "506":
            case "507":
            case "508":
            case "509":
            case "510":
            case "511":
                return "34";
            default:
                return null;
        }
    }
}
